{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas的数据转换函数map、apply、applymap\n",
    "\n",
    "数据转换函数对比：map、apply、applymap：\n",
    "1. map：只用于Series，实现每个值->值的映射；\n",
    "2. apply：用于Series实现每个值的处理，用于Dataframe实现某个轴的Series的处理；\n",
    "3. applymap：只能用于DataFrame，用于处理该DataFrame的每个元素；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. map用于Series值的转换\n",
    "\n",
    "实例：将股票代码英文转换成中文名字\n",
    "\n",
    "Series.map(dict) or Series.map(function)均可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BABA</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BABA</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司      收盘      开盘       高       低    交易量   涨跌幅\n",
       "0  2019-10-03  BIDU  104.32  102.35  104.73  101.15   2.24  0.02\n",
       "1  2019-10-02  BIDU  102.62  100.85  103.24   99.50   2.69  0.01\n",
       "2  2019-10-01  BIDU  102.00  102.80  103.26  101.00   1.78 -0.01\n",
       "3  2019-10-03  BABA  169.48  166.65  170.18  165.00  10.39  0.02\n",
       "4  2019-10-02  BABA  165.77  162.82  166.88  161.90  11.60  0.00"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "stocks = pd.read_excel('./datas/stocks/互联网公司股票.xlsx')\n",
    "stocks.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['BIDU', 'BABA', 'IQ', 'JD'], dtype=object)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks[\"公司\"].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 公司股票代码到中文的映射，注意这里是小写\n",
    "dict_company_names = {\n",
    "    \"bidu\": \"百度\",\n",
    "    \"baba\": \"阿里巴巴\",\n",
    "    \"iq\": \"爱奇艺\", \n",
    "    \"jd\": \"京东\"\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 方法1：Series.map(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks[\"公司中文1\"] = stocks[\"公司\"].str.lower().map(dict_company_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BABA</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BABA</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司      收盘      开盘       高       低    交易量   涨跌幅 公司中文1\n",
       "0  2019-10-03  BIDU  104.32  102.35  104.73  101.15   2.24  0.02    百度\n",
       "1  2019-10-02  BIDU  102.62  100.85  103.24   99.50   2.69  0.01    百度\n",
       "2  2019-10-01  BIDU  102.00  102.80  103.26  101.00   1.78 -0.01    百度\n",
       "3  2019-10-03  BABA  169.48  166.65  170.18  165.00  10.39  0.02  阿里巴巴\n",
       "4  2019-10-02  BABA  165.77  162.82  166.88  161.90  11.60  0.00  阿里巴巴"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 方法2：Series.map(function)\n",
    "\n",
    "function的参数是Series的每个元素的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks[\"公司中文2\"] = stocks[\"公司\"].map(lambda x : dict_company_names[x.lower()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "      <th>公司中文2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BABA</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BABA</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司      收盘      开盘       高       低    交易量   涨跌幅 公司中文1 公司中文2\n",
       "0  2019-10-03  BIDU  104.32  102.35  104.73  101.15   2.24  0.02    百度    百度\n",
       "1  2019-10-02  BIDU  102.62  100.85  103.24   99.50   2.69  0.01    百度    百度\n",
       "2  2019-10-01  BIDU  102.00  102.80  103.26  101.00   1.78 -0.01    百度    百度\n",
       "3  2019-10-03  BABA  169.48  166.65  170.18  165.00  10.39  0.02  阿里巴巴  阿里巴巴\n",
       "4  2019-10-02  BABA  165.77  162.82  166.88  161.90  11.60  0.00  阿里巴巴  阿里巴巴"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. apply用于Series和DataFrame的转换\n",
    "\n",
    "* Series.apply(function), 函数的参数是每个值\n",
    "* DataFrame.apply(function), 函数的参数是Series"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Series.apply(function)\n",
    "\n",
    "function的参数是Series的每个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks[\"公司中文3\"] = stocks[\"公司\"].apply(\n",
    "    lambda x : dict_company_names[x.lower()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "      <th>公司中文2</th>\n",
       "      <th>公司中文3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BABA</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BABA</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司      收盘      开盘       高       低    交易量   涨跌幅 公司中文1 公司中文2  \\\n",
       "0  2019-10-03  BIDU  104.32  102.35  104.73  101.15   2.24  0.02    百度    百度   \n",
       "1  2019-10-02  BIDU  102.62  100.85  103.24   99.50   2.69  0.01    百度    百度   \n",
       "2  2019-10-01  BIDU  102.00  102.80  103.26  101.00   1.78 -0.01    百度    百度   \n",
       "3  2019-10-03  BABA  169.48  166.65  170.18  165.00  10.39  0.02  阿里巴巴  阿里巴巴   \n",
       "4  2019-10-02  BABA  165.77  162.82  166.88  161.90  11.60  0.00  阿里巴巴  阿里巴巴   \n",
       "\n",
       "  公司中文3  \n",
       "0    百度  \n",
       "1    百度  \n",
       "2    百度  \n",
       "3  阿里巴巴  \n",
       "4  阿里巴巴  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### DataFrame.apply(function)\n",
    "\n",
    "function的参数是对应轴的Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks[\"公司中文4\"] = stocks.apply(\n",
    "    lambda x : dict_company_names[x[\"公司\"].lower()], \n",
    "    axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意这个代码：  \n",
    "1、apply是在stocks这个DataFrame上调用；  \n",
    "2、lambda x的x是一个Series，因为指定了axis=1所以Seires的key是列名，可以用x['公司']获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "      <th>公司中文2</th>\n",
       "      <th>公司中文3</th>\n",
       "      <th>公司中文4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "      <td>0.02</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "      <td>0.01</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BABA</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "      <td>0.02</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BABA</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "      <td>0.00</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司      收盘      开盘       高       低    交易量   涨跌幅 公司中文1 公司中文2  \\\n",
       "0  2019-10-03  BIDU  104.32  102.35  104.73  101.15   2.24  0.02    百度    百度   \n",
       "1  2019-10-02  BIDU  102.62  100.85  103.24   99.50   2.69  0.01    百度    百度   \n",
       "2  2019-10-01  BIDU  102.00  102.80  103.26  101.00   1.78 -0.01    百度    百度   \n",
       "3  2019-10-03  BABA  169.48  166.65  170.18  165.00  10.39  0.02  阿里巴巴  阿里巴巴   \n",
       "4  2019-10-02  BABA  165.77  162.82  166.88  161.90  11.60  0.00  阿里巴巴  阿里巴巴   \n",
       "\n",
       "  公司中文3 公司中文4  \n",
       "0    百度    百度  \n",
       "1    百度    百度  \n",
       "2    百度    百度  \n",
       "3  阿里巴巴  阿里巴巴  \n",
       "4  阿里巴巴  阿里巴巴  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. applymap用于DataFrame所有值的转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub_df = stocks[['收盘', '开盘', '高', '低', '交易量']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>104.32</td>\n",
       "      <td>102.35</td>\n",
       "      <td>104.73</td>\n",
       "      <td>101.15</td>\n",
       "      <td>2.24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>102.62</td>\n",
       "      <td>100.85</td>\n",
       "      <td>103.24</td>\n",
       "      <td>99.50</td>\n",
       "      <td>2.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>102.00</td>\n",
       "      <td>102.80</td>\n",
       "      <td>103.26</td>\n",
       "      <td>101.00</td>\n",
       "      <td>1.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>169.48</td>\n",
       "      <td>166.65</td>\n",
       "      <td>170.18</td>\n",
       "      <td>165.00</td>\n",
       "      <td>10.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>165.77</td>\n",
       "      <td>162.82</td>\n",
       "      <td>166.88</td>\n",
       "      <td>161.90</td>\n",
       "      <td>11.60</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       收盘      开盘       高       低    交易量\n",
       "0  104.32  102.35  104.73  101.15   2.24\n",
       "1  102.62  100.85  103.24   99.50   2.69\n",
       "2  102.00  102.80  103.26  101.00   1.78\n",
       "3  169.48  166.65  170.18  165.00  10.39\n",
       "4  165.77  162.82  166.88  161.90  11.60"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>104</td>\n",
       "      <td>102</td>\n",
       "      <td>104</td>\n",
       "      <td>101</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>102</td>\n",
       "      <td>100</td>\n",
       "      <td>103</td>\n",
       "      <td>99</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>103</td>\n",
       "      <td>101</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>169</td>\n",
       "      <td>166</td>\n",
       "      <td>170</td>\n",
       "      <td>165</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>165</td>\n",
       "      <td>162</td>\n",
       "      <td>166</td>\n",
       "      <td>161</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>165</td>\n",
       "      <td>168</td>\n",
       "      <td>168</td>\n",
       "      <td>163</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>16</td>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "      <td>15</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>15</td>\n",
       "      <td>16</td>\n",
       "      <td>16</td>\n",
       "      <td>15</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>28</td>\n",
       "      <td>28</td>\n",
       "      <td>28</td>\n",
       "      <td>27</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>28</td>\n",
       "      <td>28</td>\n",
       "      <td>28</td>\n",
       "      <td>27</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>28</td>\n",
       "      <td>28</td>\n",
       "      <td>28</td>\n",
       "      <td>27</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     收盘   开盘    高    低  交易量\n",
       "0   104  102  104  101    2\n",
       "1   102  100  103   99    2\n",
       "2   102  102  103  101    1\n",
       "3   169  166  170  165   10\n",
       "4   165  162  166  161   11\n",
       "5   165  168  168  163   14\n",
       "6    16   15   16   15   10\n",
       "7    15   15   15   15    8\n",
       "8    15   16   16   15   11\n",
       "9    28   28   28   27    8\n",
       "10   28   28   28   27    9\n",
       "11   28   28   28   27   10"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将这些数字取整数，应用于所有元素\n",
    "sub_df.applymap(lambda x : int(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 直接修改原df的这几列\n",
    "stocks.loc[:, ['收盘', '开盘', '高', '低', '交易量']] = sub_df.applymap(lambda x : int(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>日期</th>\n",
       "      <th>公司</th>\n",
       "      <th>收盘</th>\n",
       "      <th>开盘</th>\n",
       "      <th>高</th>\n",
       "      <th>低</th>\n",
       "      <th>交易量</th>\n",
       "      <th>涨跌幅</th>\n",
       "      <th>公司中文1</th>\n",
       "      <th>公司中文2</th>\n",
       "      <th>公司中文3</th>\n",
       "      <th>公司中文4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>104</td>\n",
       "      <td>102</td>\n",
       "      <td>104</td>\n",
       "      <td>101</td>\n",
       "      <td>2</td>\n",
       "      <td>0.02</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102</td>\n",
       "      <td>100</td>\n",
       "      <td>103</td>\n",
       "      <td>99</td>\n",
       "      <td>2</td>\n",
       "      <td>0.01</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2019-10-01</td>\n",
       "      <td>BIDU</td>\n",
       "      <td>102</td>\n",
       "      <td>102</td>\n",
       "      <td>103</td>\n",
       "      <td>101</td>\n",
       "      <td>1</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "      <td>百度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2019-10-03</td>\n",
       "      <td>BABA</td>\n",
       "      <td>169</td>\n",
       "      <td>166</td>\n",
       "      <td>170</td>\n",
       "      <td>165</td>\n",
       "      <td>10</td>\n",
       "      <td>0.02</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2019-10-02</td>\n",
       "      <td>BABA</td>\n",
       "      <td>165</td>\n",
       "      <td>162</td>\n",
       "      <td>166</td>\n",
       "      <td>161</td>\n",
       "      <td>11</td>\n",
       "      <td>0.00</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "      <td>阿里巴巴</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           日期    公司   收盘   开盘    高    低  交易量   涨跌幅 公司中文1 公司中文2 公司中文3 公司中文4\n",
       "0  2019-10-03  BIDU  104  102  104  101    2  0.02    百度    百度    百度    百度\n",
       "1  2019-10-02  BIDU  102  100  103   99    2  0.01    百度    百度    百度    百度\n",
       "2  2019-10-01  BIDU  102  102  103  101    1 -0.01    百度    百度    百度    百度\n",
       "3  2019-10-03  BABA  169  166  170  165   10  0.02  阿里巴巴  阿里巴巴  阿里巴巴  阿里巴巴\n",
       "4  2019-10-02  BABA  165  162  166  161   11  0.00  阿里巴巴  阿里巴巴  阿里巴巴  阿里巴巴"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
